﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Complaint.Models;
using Complaint.Models.Dtos;

namespace Complaint.Services
{
    public class OrganisationHelper
    {
        public static void SyncAlias(Organisation organisation, string[] aliases)
        {
            var aliasesForDelete = new List<OrganisationAlias>();

            //populate the delete category list
            foreach (var alias in organisation.OrganisationAlias)
            {
                if (!((IList)aliases).Contains(alias.Alias))
                    aliasesForDelete.Add(alias);
            }

            var model = new OrganisationAliasModel();
            foreach (var a in aliasesForDelete)
            {
                model.Delete(a);
            }

            foreach (var a in aliases)
            {
                var s = a;
                if (!organisation.OrganisationAlias.Any(alias => alias.Alias == s))
                    organisation.OrganisationAlias.Add(
                        new OrganisationAlias { Alias = a }
                        );
            }
        }

        public static void SyncCategories(
            Organisation organisation,
            ICollection<Category> categories)
        {
            var categoriesForDelete = new List<Category>();

            //populate the delete category list
            foreach (var c in organisation.Category)
            {
                if (!categories.Contains(c))
                    categoriesForDelete.Add(c);
            }

            foreach (var c in categoriesForDelete)
            {
                organisation.Category.Remove(c);
            }

            foreach (var c in categories)
            {
                if (!organisation.Category.Contains(c))
                    organisation.Category.Add(c);
            }
        }
    }
}